package com.veo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.veo.entity.*;
import com.veo.mapper.OrderMapper;
import com.veo.service.AddressService;
import com.veo.service.BookService;
import com.veo.service.CartService;
import com.veo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private CartService cartService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private BookService bookService;

    /**
     * 确认订单
     */
    @RequestMapping("/confirm")
    public String confirm(String ids, HttpSession session, Model model){
        //根据用户勾选的id查询记录
        List<CartVo> cartList = cartService.findCartVoByIds(ids);
        User user = (User) session.getAttribute("user");
        //获取当前用户的收货的地址
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        List<Address> addressList = addressService.list(queryWrapper);
        //将购买的商品信息添加到session中
        session.setAttribute("cartVos",cartList);

        model.addAttribute("list",cartList);
        model.addAttribute("addressList",addressList);
        return "confirm_order";
    }


    /**
     *提交订单
     */
    @RequestMapping("/commitOrder")
    public String commitOrder(Integer addressId,HttpSession session){
        List<CartVo> cartVos = (List<CartVo>) session.getAttribute("cartVos");
        String flag = orderService.buy(cartVos,addressId,session);
        if (flag.equals("success")){
            return "redirect:/order/list";
        }else {
            return "redirect:/book/index";
        }
    }

    /**
     * 显示用户订单列表
     */
    @RequestMapping("/list")
    public String list(){
        return "order_list";
    }

    /**
     * 获取订单页面的数据
     */
    @RequestMapping("/getOrderListData")
    public String getOrderListData(HttpSession session,Model model,OrderQueryVo orderQueryVo){
        User user = (User) session.getAttribute("user");
        List<Order> orders = orderService.findUserOrder(user.getId(),orderQueryVo);
        model.addAttribute("orders",orders);
        model.addAttribute("pre",orderQueryVo.getPage() - 1);
        model.addAttribute("next",orderQueryVo.getPage() + 1);
        model.addAttribute("cur",orderQueryVo.getPage());
        model.addAttribute("pages",orderService.findUserOrderPages(orderQueryVo));
        model.addAttribute("pageSize",orderQueryVo.getPageSize());
        return "orderData";
    }

    /**
     * 立即购买
     * @param id
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/buy")
    public String buy(String id,Integer count, HttpSession session, Model model){
        //根据用户勾选的id查询记录
        CartVo cartVo = new CartVo();
        Book book = bookService.getById(id);
        cartVo.setBookId(book.getId());
        cartVo.setId(1);
        cartVo.setCount(count);
        cartVo.setBookName(book.getName());
        cartVo.setImgUrl(book.getImgUrl());
        cartVo.setNewPrice(book.getNewPrice());

        User user = (User) session.getAttribute("user");
        cartVo.setUserId(user.getId());

        //获取当前用户的收货的地址
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        List<Address> addressList = addressService.list(queryWrapper);
        //将购买的商品信息添加到session中
        List<CartVo> list = new ArrayList<>();
        list.add(cartVo);
        UserCartVo userCartVo = cartService.wrapCart(list);
        session.setAttribute("cartVos",list);
        session.setAttribute("userCartInfo",userCartVo);

        model.addAttribute("list",cartVo);
        model.addAttribute("addressList",addressList);
        return "confirm_order";
    }
}
